package org.spring.springboot.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

@ControllerAdvice
public class GlobalDefaultExceptionHandler {
	
	// 全局异常处理返回字符串
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public JSON exception(Exception e){
		 JSONObject result =new JSONObject();
	      
		if(e instanceof ArithmeticException){
			result.put("codeNumber", "1");
		
		}
		else if(e instanceof NullPointerException)
		{
			result.put("codeNumber", "2");
		}
		else
		{
			result.put("codeNumber", "0");
		}
		result.put("message", e.getMessage());
		
		return result;
	}
}
